<?php defined('SYSPATH') or die('No direct script access.');
 
class Controller_User extends Controller_User_Common
{
    public function entry()
    {
        $users = ORM::factory('user')
            ->where( 'login', '=', Cookie::get('login', 'no user'))
            ->find();
        if($users->loaded())
        {
            if($users->accesses==2)
            {
                if(isset($_POST['exit']))
                {
                    Cookie::delete('login');
                    Cookie::delete('pas');
                    Cookie::delete('id');
                    $this->redirect('entry/login');
                }
            }
            else
            {
                $this->redirect('entry/login');
            }
        }
        else
        {
            $this->redirect('entry/login');
        }
    }
    public function action_us()
    {
        $this->entry(); 
        $sidebar = View::factory('user/us');
        $this->template->sidebar = $sidebar;
        $content = View::factory('/user/hello');
        $this->template->content = $content;
    }
    
    public function action_show()
    {
        $this->entry();
        if(isset($_POST["det"]))
        {
            $a=$_POST["idap"];
            Cookie::set('temp', $a);       
            $this->redirect('user/details');
        }
        
        $sidebar = View::factory('user/us');
        $this->template->sidebar = $sidebar;
        $content = View::factory('user/myapartment');
        $this->template->content = $content;     
    }
    public function action_details()
    { 
        $this->entry();
        $apartments = ORM::factory('apartment')
                ->where( 'id', '=', Cookie::get('temp','no user'))
                ->find();
            if($apartments->loaded())
            {
                $data = array(
                    'id' => $apartments->id,
                    'region' => $apartments->city->region->name,
                    'city' => $apartments->city->name,
                    'street' => $apartments->street,
                    'house' => $apartments->house,
                    'floor' => $apartments->floor,
                    'number' => $apartments->number,
                    'numberRoom' => $apartments->numberRoom,
                    'area' => $apartments->area,
                    'price' => $apartments->price,
                    'description' => $apartments->description,     
                    'photo1' => $apartments->photo->photo1,
                    'photo2' => $apartments->photo->photo2,
                    'photo3' => $apartments->photo->photo3
                );          
            }
        if(isset($_POST["delete"]))
        {
            ORM::factory('apartment', $apartments->id)
                ->delete();
             $this->redirect('user/show');
            
        } 
        
        $content = View::factory('user/details', $data);
        $this->template->content = $content;
        $sidebar = View::factory('user/us');
        $this->template->sidebar = $sidebar;
    
    }
    private function uploads_dir()
    {
        return DOCROOT . 'uploads/images' . DIRECTORY_SEPARATOR;
    }
    
    public function action_add()
    {
        $this->entry();
        if(isset($_POST['add']))
        {
            $apartments = ORM::factory('apartment');
            $photos = ORM::factory('photo'); 
            $apartments->city_id = $_POST['city'];    
            $apartments->user_id = Cookie::get('id');
            $apartments->street = $_POST['street'];
            $apartments->house = $_POST['house'];
            $apartments->floor = $_POST['floor'];
            $apartments->number = $_POST['number'];
            $apartments->numberRoom = $_POST['numberRoom'];
            $apartments->area = $_POST['area'];
            $apartments->price = $_POST['price'];
            $apartments->description = $_POST['description'];                              
            $prefix = "img";
            $new_name1 = $prefix . uniqid().".jpg";
            $new_name2 = $prefix . uniqid().".jpg"; 
            $new_name3 = $prefix . uniqid().".jpg"; 
         
           if ($_FILES['photo1']['name'] != ''){
            $photos->photo1 = $new_name1;}
           if ($_FILES['photo2']['name'] != ''){ 
            $photos->photo2 = $new_name2;}
           if ($_FILES['photo3']['name'] != ''){
            $photos->photo3 = $new_name3;}
            
            $validation = Validation::factory($_FILES)
                ->rules( TRUE , array(
                    array('Upload::image'),
                ));
            
            if ($validation->check())
            {           
                Upload::save($validation['photo1'], $new_name1, $this->uploads_dir());
                Upload::save($validation['photo2'], $new_name2, $this->uploads_dir());
                Upload::save($validation['photo3'], $new_name3, $this->uploads_dir());
                           
                Session::instance()->set('message', 'Image is successfully uploaded');
            }
            
            $apartments->date = date("y.m.d");
            $photos->save();           
            $apartments->save();
            $apartments->photo_id = $photos->id;
            $apartments->save();
        }
        $sidebar = View::factory('user/us');
        $this->template->sidebar = $sidebar;
        $content = View::factory('user/apartment');
        $this->template->content = $content;
    }  
     public function action_info()
     {
         $this->entry();
         $content = View::factory('user/info');
         $this->template->content = $content;           
         $sidebar = View::factory('user/us');
         $this->template->sidebar = $sidebar;
    }
    public function action_search()
    {
       //  $this->entry();
       //  $content = View::factory('user/search');
       //  $this->template->content = $content;           
       //  $sidebar = View::factory('user/us');
       //  $this->template->sidebar = $sidebar;
           $this->redirect('home/index');
    }
    
 } //end Controller_User 